﻿Imports System.Runtime.InteropServices
Imports System.IO

Public Class Util

    Private Declare Function DrawIcon Lib "user32 " (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    <DllImport("shell32.dll")> _
     Private Shared Function ExtractIcon(ByVal hInst As IntPtr, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As IntPtr
    End Function

    Public Shared Function GetCurrentFolder() As String
        Return System.Windows.Forms.Application.StartupPath
    End Function

    Public Shared Function GetCurrentFolderIcon() As String
        Return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "icon")
    End Function

    Public Shared Function GetCurrentFolderSoftWare() As String
        Return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "SoftWare")
    End Function

    Public Shared Function GetCurrentFileIcon(ByVal filename As String, ByVal pathType As Integer) As String

        If pathType = 0 Then
            Return System.IO.Path.Combine(GetCurrentFolderIcon, filename)
        Else
            Return filename
        End If

    End Function

    Public Shared Function GetCurrentExePath(ByVal filename As String, ByVal pathType As Integer) As String

        If pathType = 0 Then
            Return System.IO.Path.Combine(GetCurrentFolder, "SoftWare\" + filename)
        Else
            Return filename
        End If

    End Function

    Public Shared Function GetCurrentFolderDb() As String
        Return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "data")
    End Function

    Public Shared Function GetCurrentFileType() As String
        Return System.IO.Path.Combine(GetCurrentFolderDb, "type.txt")
    End Function

    Public Shared Function GetSchemaType() As DataTable
        Dim dt As New DataTable

        dt.Columns.Add("id")
        dt.Columns.Add("name")
        dt.Columns.Add("image", GetType(Image))
        dt.Columns.Add("imagepath")
        Return dt

    End Function

    Public Shared Function GetSoftWareType(ByVal bigTypeID As String) As DataTable

        Dim dt As DataTable = Util.GetSchemaType
        Dim _XMLSoftWareType As New XMLSoftWareType
        Dim tys As List(Of CSoftWareType)
        If bigTypeID.Trim = "" Then
            tys = _XMLSoftWareType.GetAll()
        Else
            tys = _XMLSoftWareType.GetByBigTypeId(bigTypeID)
        End If

        For Each ty As CSoftWareType In tys
            Dim imPath As String = Util.GetCurrentFileIcon(ty.IconPath, CInt(ty.IconpathType))
            Dim im As Image
            Try
                im = Image.FromFile(imPath).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            Catch ex As Exception
                im = My.Resources.LOGO.ToBitmap
            End Try
            dt.Rows.Add(New Object() {ty.Id, ty.Name, im, imPath})
        Next

        Return dt

    End Function

    Public Shared Function GetSoftWareBigType() As DataTable

        Dim dt As DataTable = Util.GetSchemaType
        Dim _XMLSoftWareBigType As New XMLSoftWareBigType
        Dim tys As List(Of CSoftWareBigType) = _XMLSoftWareBigType.GetAll
        For Each ty As CSoftWareBigType In tys
            Dim imPath As String = Util.GetCurrentFileIcon(ty.IconPath, CInt(ty.IconpathType))
            Dim im As Image
            Try
                im = Image.FromFile(imPath).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            Catch ex As Exception
                im = My.Resources.LOGO.ToBitmap
            End Try
            dt.Rows.Add(New Object() {ty.Id, ty.Name, im, imPath})
        Next

        Return dt

    End Function

    Public Shared Function GetSoftWareBigTypeXml() As String
        Return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "data\SoftWareBigType.xml")
    End Function

    Public Shared Function GetSoftWareTypeXml() As String
        Return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "data\SoftWareType.xml")
    End Function

    Public Shared Function GetSoftWareXml() As String
        Return System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "data\SoftWare.xml")
    End Function

    Public Shared Function GetExeIcon(ByVal exepath As String) As Image

        Dim ic As Icon = Icon.ExtractAssociatedIcon(exepath)

        Return CType(ic.ToBitmap, Image)

    End Function

    Public Shared Function GetExeSize(ByVal exepath As String) As String

        If System.IO.File.Exists(exepath) Then
            Dim fifinof As New System.IO.FileInfo(exepath)
            If fifinof.Length < 1024 Then
                Return CInt(fifinof.Length) & "Byte"
            ElseIf fifinof.Length < 1024 * 1024 Then
                Return CInt(fifinof.Length / 1024) & "KB"
            Else
                Return CInt(fifinof.Length / (1024 * 1024)) & "MB"
            End If
        End If

        Return ""

    End Function

    Public Shared Function GetSaveFileName(ByVal exepath As String, ByVal pathtype As Integer)
        If pathtype = 1 Then
            Return exepath
        Else
            Dim so As String = GetCurrentFolderSoftWare()
            If Not so.EndsWith("\") Then
                so &= "\"
            End If
            Return exepath.ToLower.Replace(so.ToLower, "").Trim
        End If
    End Function

    Public Shared Sub CenterForm(ByVal frm As Form)
        frm.Left = (Screen.PrimaryScreen.WorkingArea.Width - frm.Width) / 2
        frm.Top = (Screen.PrimaryScreen.WorkingArea.Height - frm.Height) / 2
    End Sub

    Public Shared Sub CreateBigTypeTemple()
        If Not File.Exists(GetSoftWareBigTypeXml) Then
            Dim sb As New System.Text.StringBuilder
            sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
            sb.AppendLine("<SoftWareOperate>")
            sb.AppendLine("</SoftWareOperate>")
            File.WriteAllText(GetSoftWareBigTypeXml, sb.ToString)
        End If
    End Sub

    Public Shared Sub CreateTypeTemple()
        If Not File.Exists(GetSoftWareTypeXml) Then
            Dim sb As New System.Text.StringBuilder
            sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
            sb.AppendLine("<SoftWareOperate>")
            sb.AppendLine("</SoftWareOperate>")
            File.WriteAllText(GetSoftWareTypeXml, sb.ToString)
        End If
    End Sub

    Public Shared Sub CreateSoftWareTemple()
        If Not File.Exists(GetSoftWareXml) Then
            Dim sb As New System.Text.StringBuilder
            sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
            sb.AppendLine("<SoftWareOperate>")
            sb.AppendLine("</SoftWareOperate>")
            File.WriteAllText(GetSoftWareXml, sb.ToString)
        End If
    End Sub

    Public Shared Function GetAppConfigTemple() As String
        Dim sb As New System.Text.StringBuilder
        sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
        sb.AppendLine("<configuration>")
        sb.AppendLine("  <appSettings>")
        sb.AppendLine("    <add key=""UserSkin"" value=""1"" />")
        sb.AppendLine("    <add key=""mode"" value=""1"" />")
        sb.AppendLine("    <add key=""FormWidth"" value=""600"" />")
        sb.AppendLine("    <add key=""FormHeight"" value=""500"" />")
        sb.AppendLine("    <add key=""LeftPanelWidth"" value=""130"" />")
        sb.AppendLine("    <add key=""Title"" value=""软件管理"" />")
        sb.AppendLine("    <add key=""SkinName"" value=""vista"" />")
        sb.AppendLine("  </appSettings>")
        sb.AppendLine("</configuration>")
        Return sb.ToString
    End Function
End Class
